<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){
        $title = '登入 - Fly社区';
        $this->assign('title',$title); 
        $question = D('vercode')->getOne();
        $this->assign('question',$question);
        $this->display();
    }

    public function checkdata()
    {
        if ($_POST) {
            $this->checkVercode();

            $get_post = I('post.');

            if ($get_post['email'] != '') {
                if (!preg_match('/^[a-z0-9][a-z0-9_]{2,}@([a-z0-9]+|[a-z0-9][a-z0-9-]+[a-z0-9])([\.][a-z0-9]+)+$/', $get_post['email'])) {
                    $arr = array('error'=>'email不合要求');
                    echo json_encode($arr);
                    exit;
                } else {
                    if (M('Users')->where('email="'.$get_post['email'].'"')->find()) {
                        if ($get_post['pass'] != '') {
                            if (!preg_match('/^[\S]{6,12}$/', $get_post['pass'])) {
                                $arr = array('error'=>'密码长度不合要求');
                                echo json_encode($arr);
                                exit;
                            } else {
                                $condition = ['upwd' => md5($get_post['pass']),
                                              'email' => $get_post['email'],
                                              'available' => 1];
                                $result = D('users')->relation(true)->where($condition)->find();
                                if ($result) {
                                    $data['login_time'] = time();
                                    M('users')->where('uid="'.$result['uid'].'"')->save($data);
                                    $arr = array('info'=>'登录成功');
                                    setcookie("nickname", $result['uname'], time()+31536000, '/');
                                    $_SESSION['email'] = $result['email'];
                                    $_SESSION['nickname'] = $result['uname'];
                                    $_SESSION['uid'] = $result['uid'];
                                    $_SESSION['avatar'] = $result['uext']['avatar'];
                                    unset($_SESSION['answer']);//清除验证答案
                                    echo json_encode($arr);
                                    exit;
                                } else {
                                    $arr = array('error'=>'密码不正确');
                                    echo json_encode($arr);
                                    exit;
                                }
                            }
                        } else {
                            $arr = array('error'=>'密码不能为空');
                            echo json_encode($arr);
                            exit;
                        }
                    } else {
                        $arr = array('error'=>'email不存在');
                        echo json_encode($arr);
                        exit;
                    }
                }
            } else {
                $arr = array('error'=>'email不能为空');
                echo json_encode($arr);
                exit;
            }
        } else {
            $this->error('非法访问','/home/index/index', 1);
        }
    }
    //qq登录函数
    public function qqlogin()
    {
        $sql = 'select qq_login from sys_config';
        $qq_switch = M()->query($sql);
        if ($qq_login_switch) {
            qq_login();
        } else {
            exit;
        }
    }
    //获取qq登录返回的授权信息
    public function getqqlogin()
    {
        $openid = getOpenid();
        // 查询是否已经绑定QQ登录
        $sql = "select t1.uid,t1.uname,t1.email,t2.avatar from users t1 left join users_ext t2 on t1.uid=t2.uid where t2.qq='{$openid}'";

        $res = M()->query($sql);

        if($res){
            $_SESSION['nickname']=$res[0]['uname'];
            $_SESSION['uid']=$res[0]['uid'];
            $_SESSION['avatar']=$res[0]['avatar'];
            $_SESSION['email']=$res[0]['email'];
            $this->success('登录成功',U('home/index/index'));
        }else{
            if (!$_SESSION['uid']) {
                $info = getUserInfo();
                $info['qq'] = $openid;
                $res = A('Reg')->QQReg($info);

                if ($res) {
                    $_SESSION['uid'] = $res;
                    $this->success('QQ号登录成功',U('home/ucenter/index'));
                } else {
                    $this->error('QQ号登录失败',U('home/ucenter/index'));
                }
            } else {
                $info['uext'] = array('qq' => $openid);
                $res = D('users')->where('uid='.$_SESSION['uid'])->save($info);
                $this->success('QQ号绑定成功',U('home/index/index'));
            }
        } 
    }
    //微博登录函数
    public function wblogin()
    {
        # code...
    }
}